package com.example.core;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public abstract class WebInfoCrawler<T> implements WebCrawler<T> {
    protected final Document doc;
    private final String html;

    public WebInfoCrawler(Document doc) {
        this.doc = doc;
        this.html = doc.html();
    }

    public static Document parseHtml(String html) {
        return Jsoup.parse(html);
    }

    public static Document getUrl(String url) throws IOException {
        return Jsoup.connect(url).get();
    }

    protected void init() throws Exception {
    }

    @Override
    public String getContent() {
        return html;
    }

    protected abstract T parse() throws Exception;

    public T execute() throws Exception {
        this.init();
        return parse();
    }
}
